package com.stedu.mall.userservice.service;

import cn.hutool.crypto.SecureUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.stedu.mall.common.bean.User;
import com.stedu.mall.common.exception.SteduException;
import com.stedu.mall.common.service.UserService;
import com.stedu.mall.common.utils.UUIDUtil;
import com.stedu.mall.userservice.mapper.UserMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@DubboService
@Component
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public boolean reg(User user) throws SteduException {
        //判断用户名在系统中是否存在
        if (userMapper.selectByUsername(user.getUsername()) != null) {
            throw new SteduException("用户名已被使用，请更改重试");
        }
        //生成盐
        String salt = UUIDUtil.get8UUID();
        user.setSalt(salt);
        //对密码进行加密
        String password = SecureUtil.md5(SecureUtil.md5(user.getPassword() + salt));
        user.setPassword(password);
        user.setPayPassword(password);//支付密码和密码一样默认
        user.setStatus(1);
        user.setRegTime(new Date());
        user.setMoney(new BigDecimal(0));
        //添加用户
        return userMapper.insert(user) == 1;
    }

    @Override
    public User login(String username, String password) throws SteduException {
        //根据用户名查询
        User user = userMapper.selectByUsername(username);
        if (user == null) {
            throw new SteduException("用户名错误，请重新登录");
        }
        if (user.getStatus().equals(2) || user.getStatus().equals(3)) {
            throw new SteduException("您的账号已被禁用或注销哦~~~");
        }
        //对用户输入的密码进行加密--使用MD5与盐加密
        String s = SecureUtil.md5(SecureUtil.md5(password + user.getSalt()));
        //对加密之后的密码与数据库中的密码进行比较
        if (!s.equals(user.getPassword())) {
            throw new SteduException("密码错误，请重新登录");
        }
        return user;
    }

    @Override
    public boolean delete(Integer id) throws SteduException {
        if (userMapper.selectById(id) == null) {
            throw new SteduException("没有这个用户，无法禁用");
        }
        return userMapper.delete(id) == 1;
    }

    @Override
    //充值用的
    public boolean update(User user, Integer userId) throws SteduException {
        User u = userMapper.selectById(userId);
        user.setId(userId);
        user.setMoney(user.getMoney().add(u.getMoney()));
        return userMapper.update(user) == 1;
    }
    @Override
    //退款用的
    public boolean update1(User user, Integer userId) throws SteduException {
        return userMapper.update(user) == 1;
    }
    @Override
    public boolean back(Integer id) {
        User user = userMapper.selectById(id);
        String salt = user.getSalt();
        User user1 = new User();
        user1.setPassword(SecureUtil.md5(SecureUtil.md5("123" + salt)));
        user1.setId(id);
        return userMapper.update(user1) == 1;
    }

    @Override
    public Object selectByCondition(User condition, Integer pageNum, Integer pageSize) {
        if (pageNum == null) {//不分页
            return userMapper.selectByCondition(condition);
        } else {//分页
            //设置分页参数
            PageHelper.startPage(pageNum, pageSize);
            //查询
            List<User> users = userMapper.selectByCondition(condition);
            //创建分页信息的对象
            PageInfo<User> pageInfo = new PageInfo<>(users);
            return pageInfo;
        }
    }

    @Override
    public User selectById(Integer id) {
        return userMapper.selectById(id);
    }

    @Override
    public boolean changePwd(String oldPwd, String newPwd, Integer id) throws SteduException {
        /*
         * 判断输入的原密码是否正确
         * 1.根据id查询用户信息，获取原密码和盐
         * 2.把输入的原密码进行加密
         * 3.比较
         * */
        //根据id查询用户信息，获取原密码和盐
        User user = userMapper.selectById(id);
        //把输入的原密码进行加密
        String s = SecureUtil.md5(SecureUtil.md5(oldPwd + user.getSalt()));
        //比较
        if (!s.equals(user.getPassword())) {
            throw new SteduException("原密码错误，请重新输入");
        }

        //对新密码结合盐进行加密
        String s1 = SecureUtil.md5(SecureUtil.md5(newPwd + user.getSalt()));
        if (s1.equals(user.getPassword())) {
            throw new SteduException("两次密码一致，请重新输入");
        } else {
            //修改密码
            user.setPassword(s1);
        }
        return userMapper.update(user) == 1;
    }

    @Override
    public boolean changePwd1(String oldPwd, String newPwd, Integer id) throws SteduException {
        /*
         * 判断输入的原支付密码是否正确
         * 1.根据id查询用户信息，获取原支付密码和盐
         * 2.把输入的原支付密码进行加密
         * 3.比较
         * */
        //根据id查询用户信息，获取原支付密码和盐
        User user = userMapper.selectById(id);
        //把输入的原支付密码进行加密
        String s = SecureUtil.md5(SecureUtil.md5(oldPwd + user.getSalt()));
        //比较
        if (!s.equals(user.getPayPassword())) {
            throw new SteduException("原支付密码错误，请重新输入");
        }

        //对新支付密码结合盐进行加密
        String s1 = SecureUtil.md5(SecureUtil.md5(newPwd + user.getSalt()));
        if (s1.equals(user.getPayPassword())) {
            throw new SteduException("两次支付密码一致，请重新输入");
        } else {
            //修改支付密码
            user.setPayPassword(s1);
        }
        return userMapper.update(user) == 1;
    }

    @Override
    public boolean updateMoney(User user,Integer userId) throws  SteduException {
        User user2 = userMapper.selectById(userId);
        if(userMapper.selectById(user2.getId()).getStatus() == 0){
            throw new SteduException("您还未认证,请认证后再进行操作");
        }
        String s = SecureUtil.md5(SecureUtil.md5(user.getPayPassword()+user2.getSalt()));
        if (!user2.getPayPassword().equals(s)) {
            throw new SteduException("支付密码错误,请重新输入");
        }
        User user1 = new User();
        user1.setId(userId);
        user1.setMoney(user2.getMoney().add(user.getMoney()));
        return userMapper.update(user1) == 1;
    }

}
